/*
 * DataSet.java
 *
 * Created on 30 de Agosto de 2007, 21:14
 *
 */

package rad.Java.Common.DB;

/**
 *
 * @author Evolution Systems - Do the Evolution
 */
import java.sql.*;	
import java.io.*;
import java.util.ArrayList;

public class DataSet implements Serializable
{
   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ArrayList<String> headings;
	public Object[][] data;
	private int top;
	private int index;
	
	public DataSet()
	{
		top=0;
		index=0;
	}

        public DataSet(int rows, int cols)
	{
		data = new Object[rows][cols];
		top=0;
		index=0;
	}
	
	public void fill(ResultSet rs) throws SQLException
	{
		int numCols, numRows, i;
		ResultSetMetaData md ;
		
		md= rs.getMetaData();
		numCols= md.getColumnCount();
		
		//Pega o n�mero de linhas == POGramaticamente falando
		rs.last();
		numRows= rs.getRow();
		
		//data = new Object[numRows][numCols];
		data = new Object[numRows][numCols];
		//headings = new String[numCols];
		
		//Get the label of the columns
		for(i= 0; i < numCols; i++)
			headings.add(md.getColumnLabel(i+1));
			
		rs.beforeFirst();
		while( rs.next()){
			//String [] row= new String[numCols];
			for(i=0; i < numCols; i++)
				data[top][i]= rs.getString(i+1);
			top++;
		}
	}

        public void addRow(String []row)
	{
		data[top++]= row;
	}

        public String[] getRow(int index)
        {
            return (String[]) data[index];
        }

        public String getData(int index, String field)
        {
            return (String) data[index][headings.indexOf(field)];
        }

	public void setHeadings(ArrayList<String> h)
	{
		this.headings= h;
	}

        public boolean next()
	{
		if( index < top){
			index++;
			return true;
		}
		return false;
	}
	
	public boolean prior()
	{
		if( index > 0){
			index--;
			return true;
		}
		return false;
	}
	
	public void first()
	{
		index=0;
	}
	
	public void last()
	{
		index= top;
	}
	
	public int rowsCount()
	{
		return top;
	}
}